import 'package:core/mvi/mvi_view_model.dart';
import 'package:domain/usecases/banner_use_case.dart';
import 'package:domain/usecases/home_article_use_case.dart';
import 'package:flutter/widgets.dart';

import 'home_tab_state.dart';

class TabViewModel
    extends MVIViewModel<HomeTabUIState, HomeTabEvent, HomeTabSideEffect> {
  final BannerUseCase _bannerUseCase;
  final HomeArticleUseCase _homeArticleUseCase;

  TabViewModel({
    required BannerUseCase bannerUseCase,
    required HomeArticleUseCase homeArticleUseCase,
  }) : _bannerUseCase = bannerUseCase,
       _homeArticleUseCase = homeArticleUseCase;

  @override
  void handleEvent(HomeTabEvent event) {
    if (event is InitEvent) {
      _processInitEvent();
    } else if (event is TestSendEvent) {
      debugPrint(event.param);
    } else if (event is TestLoadingEvent) {
      _processLoadingEvent();
    }

    if (event is NewEvent) {
      debugPrint(event.value);
    }
  }

  void _processInitEvent() async {
    debugPrint('_processInitEvent');
    updateState(state.copyWith(isLoading: true));
    final banners = await _bannerUseCase.fetchBanners();
    updateState(state.copyWith(isLoading: false, banners: banners));
    sendEffect(InitFinishedEffect(param: "init finsiheafafdaffda"));
  }

  void _processLoadingEvent() async {
    sendEffect(ShowLoadingEffect());

    await Future.delayed(Duration(seconds: 1));

    sendEffect(HideLoadingEffect());
  }

  @override
  HomeTabUIState get initialState => HomeTabUIState.initial;
}
